#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>

/*
 * 内存映射IO
 * */
int main(int argc, char **argv)
{
	int fd, count, i;
	struct stat stat_f;
	char *str;

	if(argc < 2){
		fprintf(stderr, "Stage....\n");
		exit(1);
	}

	fd = open(argv[1], O_RDONLY);
	if(fd < 0){
		perror("open()");
		exit(1);
	}
	
	if(fstat(fd, &stat_f) < 0){
		perror("fstat()");
		exit(1);
	}
	
	str = mmap(NULL, stat_f.st_size, PROT_READ, MAP_SHARED, fd, 0);
	if(str == MAP_FAILED){
		perror("mmap()");
		exit(1);
	}
	close(fd);

	for (i = 0; i < stat_f.st_size; i++) {
		if(str[i] == 'a')
			count++;
	}
	printf("count : %d \n", count);

	munmap(str, stat_f.st_size);

	exit(0);
}
